package com.coocaa.typhoon.api.v1.init;

import com.coocaa.cirrus.common.base.exception.BusinessException;
import com.coocaa.cirrus.common.third.filestore.config.TosProperties;
import com.coocaa.typhoon.api.third.volcengine.model.BaseVikingDbUserFileEntity;
import com.coocaa.typhoon.api.third.volcengine.model.VikingDbUserFileEntity;
import com.coocaa.typhoon.api.third.volcengine.service.VikingDbUserFileService;
import com.coocaa.typhoon.api.third.volcengine.util.VikingDbConvert;
import com.coocaa.typhoon.api.third.volcengine.yml.VolcConfigYml;
import com.coocaa.typhoon.api.v1.userfile.model.input.GetSearchDataModel;
import com.volcengine.service.vikingDB.CollectionClient;
import com.volcengine.service.vikingDB.IndexClient;
import com.volcengine.service.vikingDB.common.DataObject;
import com.volcengine.service.vikingDB.common.SearchParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2025/10/18 22:06
 * @description
 */
@Slf4j
@Service
public class VikingInitService implements InitializingBean {
    private CollectionClient collectionClientV1;
    private IndexClient indexClientV1;
    @Autowired
    private VikingDbUserFileService vikingDbUserFileService;
    @Autowired
    private TosProperties tosProperties;
    @Autowired
    private VolcConfigYml volcConfigYml;

    @Override
    public void afterPropertiesSet() throws Exception {
//        collectionClientV3 = new CollectionClient("user_file_test_v3", vikingDbYml.getVikingDbHost(), tosProperties.getTosRegionName(), tosProperties.getTosSecretId(), tosProperties.getTosSecretKey(), "https");
//        indexClientV3 = new IndexClient("user_file_test_v3", "user_file_test_v3_index", vikingDbYml.getVikingDbHost(), tosProperties.getTosRegionName(), tosProperties.getTosSecretId(), tosProperties.getTosSecretKey(), "https");

        collectionClientV1 = new CollectionClient("user_file_test", volcConfigYml.getVikingDbHost(), tosProperties.getTosRegionName(), tosProperties.getTosSecretId(), tosProperties.getTosSecretKey(), "https");
        indexClientV1 = new IndexClient("user_file_test", "tos_link_index", volcConfigYml.getVikingDbHost(), tosProperties.getTosRegionName(), tosProperties.getTosSecretId(), tosProperties.getTosSecretKey(), "https");
    }

    public void init(GetSearchDataModel dataModel) throws Exception {
        SearchParam param = getSearchParam(dataModel);
        List<DataObject> dataObjectList = indexClientV1.search(param);
        List<VikingDbUserFileEntity> entityList = convertVikingDbUserFileEntityList(dataObjectList);
        vikingDbUserFileService.upsertData(entityList);
    }


    public static List<VikingDbUserFileEntity> convertVikingDbUserFileEntityList(List<DataObject> dataObjectList) {
        List<VikingDbUserFileEntity> res = new ArrayList<>();
        dataObjectList.forEach(dataObject -> {
            Double score = dataObject.getScore();
            VikingDbUserFileEntity entity = VikingDbConvert.convertMapToEntity(dataObject.getFields(), VikingDbUserFileEntity.class);
            if (null != entity) {
                entity.setDesc(" ");
                res.add(entity);
            }
        });
        return res;
    }

    public static SearchParam getSearchParam(GetSearchDataModel dataModel) {
        try {
            SearchParam searchParam = new SearchParam();
            Map<String, Object> filterMap = VikingDbConvert.getFilerMap(dataModel);
            searchParam.setFilter(filterMap);
            searchParam.setOutputFields(VikingDbConvert.getColumnNames(BaseVikingDbUserFileEntity.class));
            if (null != dataModel.getLimit()) {
                searchParam.setLimit(dataModel.getLimit());
            }
            return searchParam.build();
        } catch (Exception e) {
            log.error("getSearchParam", e);
            throw new BusinessException("构建查询参数异常");
        }
    }

}
